package root.electronclass.electronclass.school

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentSchoolComplaintsBoxBinding
import root.electronclass.electronclass.databinding.FragmentSchoolComplaintsBoxItemBinding
import root.electronclass.electronclass.databinding.FragmentSchoolComplaintsBoxItemItemBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.ext.tabs
import root.electronclass.electronclass.ext.toMMddnHHmm
import root.electronclass.electronclass.mvp.entity.GroupData
import root.electronclass.electronclass.mvp.entity.IdeaBox
import root.electronclass.electronclass.school.model.SchoolComplaintsBoxModel


class SchoolComplaintsBoxFragment : ViewBindingFragment<FragmentSchoolComplaintsBoxBinding>() {
    private val viewModel: SchoolComplaintsBoxModel by viewModels()
    private var dataType = 1

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.load(page = 1)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val tabs = resources.getStringArray(R.array.class_tabs)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                getString(R.string.class_7_day) -> dataType = 1
                getString(R.string.class_3_month) -> dataType = 2
                getString(R.string.class_current_semester) -> dataType = 3
                getString(R.string.class_all) -> dataType = 4
            }

            viewModel.load(type = dataType, text = text, 1)
        }
        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, page = 1)
            false
        }

//        var list = mutableListOf<GroupData<IdeaBox>>()

        viewModel.data.observe(viewLifecycleOwner) { its ->
            if (its.first == 1) {
                val list = its.second.groupBy { it.createTime?.take(10) }
                val mapList = mutableListOf<GroupData<IdeaBox>>()
                for (i in list) {
                    mapList.add(GroupData(i.key, i.value))
                }
                val sortedMap = mapList.sortedBy { it.createTime }
                adapter.notifyChanged(sortedMap)
            } else {
                val list = its.second.groupBy { it.createTime?.take(10) }
                val mapList = mutableListOf<GroupData<IdeaBox>>()
                for (i in list) {
                    mapList.add(GroupData(i.key, i.value))
                }
                val sortedMap = mapList.sortedBy { it.createTime }
                adapter.addAll(sortedMap)
            }
        }
    }

    private val adapter = BasicAdapter<GroupData<IdeaBox>, FragmentSchoolComplaintsBoxItemBinding> { holder, item ->
        holder.binding.itemTitle.text = item.createTime?.toMMddnHHmm
        val childAdapter = BasicAdapter<IdeaBox, FragmentSchoolComplaintsBoxItemItemBinding> { holder1, childItem ->
            holder1.binding.picSdw.setImageURI(if (childItem.pictureFormat.isNotEmpty()) childItem.pictureFormat[0].url else "")
            holder1.binding.title.text = childItem.name
            holder1.binding.name.text = childItem.created
            holder1.binding.time.text = "发布于 ${childItem.createTime}"
            holder1.binding.tvType.text = childItem.type

            holder1.binding.tvBtn.setOnClickListener {
                startFragment(MakeCommentsFragment::class.java, bundleOf(EXTRA_DATA to childItem))
            }
        }
        holder.binding.itemRecycler.addItemDecoration(GridSpacingItemDecoration(1, 24, false))
        holder.binding.itemRecycler.adapter = childAdapter
        childAdapter.refresh(item.list)
    }

}